<?php
    class Nganhhoc_model extends CI_Model{
        public function __construct() {
            parent::__construct();
            $this->load->database();
        }
        private function is_exist($colum,$value) {
            $this->db->select('id');
            $this->db->from('gs7s_nganhhoc');
            $this->db->where($colum,$value);
            $query=$this->db->get();
            if ($query->num_rows()>0) return TRUE;
            return FALSE;
        }
        public function create($name){
            if ($this->is_exist('TenNganhHoc',$name)===TRUE) return FALSE;
            $data=array(
                'TenNganhHoc'=>$name,
                'anhien'=>FALSE
            );
            $this->db->insert('gs7s_nganhhoc',$data);
            return TRUE;
        }
        public function edit($id,$name,$status) {
            if ($this->is_exist('id', $id)===FALSE) return FALSE;
            $data=array(
                'TenNganhHoc'=>$name,
                'anhien'=>$status
            );
            $this->db->update('gs7s_nganhhoc', $data, array('id'=>$id));
            return TRUE;
        }
        public function edit_name($id,$name) {
            if ($this->is_exist('id', $id)===FALSE) return FALSE;
            $data=array(
                'TenNganhHoc'=>$name,
            );
            $this->db->update('gs7s_nganhhoc', $data, array('id'=>$id));
            return TRUE;
        }
        public function edit_status($id,$status) {
            if ($this->is_exist('id', $id)===FALSE) return FALSE;
            $data=array(
                'anhien'=>$status
            );
            $this->db->update('gs7s_nganhhoc', $data, array('id'=>$id));
            return TRUE;
        }
        public function load_id($id='all') {
            $this->db->select ('*');
            $this->db->from('gs7s_nganhhoc');
            if ($id!=='all') $this->db->where ('id',$id);
            $query=$this->db->get();
            return $query->result_array();
        }
        public function load_status($status) {
            $this->db->select ('*');
            $this->db->from('gs7s_nganhhoc');
            $this->db->where('anhien',$status);
            $query=$this->db->get();
            return $query->result_array();
        }
        public function delete($id) {
            $this->db->delete('gs7s_nganhhoc', array('id'=>$id));
        }
    }